简体   繁体   English

THREE.js着色器仅在调整窗口大小后显示

[英]THREE.js shader only showing after resizing the window

I'm new to THREE.js and I'm doing some experiments with shaders. 我是THREE.js的新手,并且正在使用着色器进行一些实验。 For some reason, this one only shows up when I resize the windows. 出于某种原因, 这个时候我调整窗口的大小只有一个显示出来。 The code is here . 代码在这里 Any suggestion about why it's showing up only after resizing the windows? 关于为何仅在调整窗口大小后才显示它的任何建议?

I think the problem is because you only assign values to the resolution uniform in the function onWindowResize: 我认为问题是因为您仅在onWindowResize函数中为统一分辨率分配值:

uniforms.resolution.value.x = window.innerWidth;
uniforms.resolution.value.y = window.innerHeight;

And since when you initialise the uniform (function createContent()), you just assign to resolution a new vector (new THREE.Vector2()), it's automatically given the (0,0) coordinates. 而且,由于在初始化制服(函数createContent())时,只需将新矢量(新THREE.Vector2())分配给分辨率,便会自动获得(0,0)坐标。 Looking at your shader code, there appear then some operations dividing by 0 that seem to me the cause of not rendering when the screen appears. 查看您的着色器代码,然后出现一些除以0的操作,在我看来,这是导致屏幕出现时无法渲染的原因。

The solution just may be to assign the coordinates to the uniform resolution also when it's declared in the javascript code, as follows (function createContent()): 解决方案可能只是在javascript代码中声明坐标时也将坐标分配给统一分辨率,如下所示(函数createContent()):

var initialResolution = new THREE.Vector2(window.innerWidth,window.innerHeight);
uniforms = {time: {type: "f",  value: 1.0},
            mouseX:     {type: "f",  value: 0.0},
            mouseY:     {type: "f",  value: 0.0},
            resolution: {type: "v2", value: initialResolution}
            [...]
           }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM