Here is some code that I merged together:
"use strict";
var OrbitControls = THREE.OrbitControls,
CSS3DRenderer = THREE.CSS3DRenderer,
CSS3DObject = THREE.CSS3DObject,
Scene = THREE.Scene,
PerspectiveCamera = THREE.PerspectiveCamera,
Mesh = THREE.Mesh,
PlaneGeometry = THREE.PlaneGeometry,
MeshPhongMaterial = THREE.MeshPhongMaterial,
Color = THREE.Color,
DoubleSide = THREE.DoubleSide,
NoBlending = THREE.NoBlending,
WebGLRenderer = THREE.WebGLRenderer,
MeshBasicMaterial = THREE.MeshBasicMaterial;
var CSS3DDemo = /** @class */ (function() {
function CSS3DDemo() {
this.scene = new Scene();
this.camera = new PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 500);
this.webGLRenderer = new WebGLRenderer();
this.css3DRenderer = new CSS3DRenderer();
this.controls = new OrbitControls(this.camera, this.css3DRenderer.domElement);
this.camera.position.set(0, 0, 20);
this.webGLRenderer.setSize(window.innerWidth, window.innerHeight);
this.webGLRenderer.setClearColor(0xFFFFFF);
this.css3DRenderer.setSize(window.innerWidth, window.innerHeight);
this.css3DRenderer.domElement.style.top = '0px';
this.css3DRenderer.domElement.style.left = '0px';
this.css3DRenderer.domElement.style.position = 'absolute';
var div = window.document.createElement('img');
div.style.width = '160px';
div.style.height = 'auto';
div.src = 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/2e/1-month-old_kitten_42.jpg/2880px-1-month-old_kitten_42.jpg';
var object = new CSS3DObject(div);
object.position.set(0, 0, 0);
object.scale.set(1 / 16, 1 / 16, 1 / 16);
this.scene.add(object);
var planeGeometry = new PlaneGeometry(10, 10);
this.scene.add(this.camera);
document.getElementById("content").appendChild(this.webGLRenderer.domElement);
document.getElementById("content").appendChild(this.css3DRenderer.domElement);
this.render();
}
CSS3DDemo.prototype.render = function() {
var _this = this;
window.requestAnimationFrame(function() {
return _this.render();
});
this.css3DRenderer.render(this.scene, this.camera);
this.webGLRenderer.render(this.scene, this.camera);
this.controls.update();
};
return CSS3DDemo;
}());
new CSS3DDemo();
html,
body {
width: 100vw;
margin: 0;
height: 100vh;
padding: 0;
overflow: hidden;
border: 0;
}
#content {
width: 100%;
height: 100%;
overflow: hidden;
}
<div id="content"></div>
<script src='https://gitcdn.xyz/repo/mrdoob/three.js/dev/build/three.min.js'></script>
<script src='https://gitcdn.xyz/repo/mrdoob/three.js/dev/examples/js/controls/OrbitControls.js'></script>
<script src='https://gitcdn.xyz/repo/mrdoob/three.js/dev/examples/js/renderers/CSS3DRenderer.js'></script>
<script src="./script.js"></script>
It is based on Three.js and CSS3DRenderer.
Instead of the cat image, I would like to use divs from the HTML file for my 3D scene. For example <div>Hello World.</div>
and <div>What's up?</div>
.
Is there a way to select divs and add them into the scene?
I have tried a lot but can't find a solution.
Could somebody help me please? I would be soooo thankful!!
Rather than creating a DOM element in your code, you can add a div in the DOM, and then select it with document.querySelector, and then you can add it to the scene.
Explaination: Your code actually creates an object of type Element and gives it as a parameter to a CSS3DObject, in order to add it the threeJS scene. But you can create as many tag you want in the HTML code and then select it with Javascript. The element you get with document.querySelector is also an object of type Element. You can read more about it here: https://developer.mozilla.org/fr/docs/Web/API/Document/querySelector There's also several ways to select elements with Javascript (by Id, class, or tag by example...): https://plainjs.com/javascript/selecting/
SOLUTION:
JS:
var div = document.querySelector("#stuff");
div.style.width = '160px';
div.style.height = 'auto';
var object = new CSS3DObject(div);
Html:
<div id="stuff">Hello from dom</div>
Working example using your code:
"use strict";
var OrbitControls = THREE.OrbitControls,
CSS3DRenderer = THREE.CSS3DRenderer,
CSS3DObject = THREE.CSS3DObject,
Scene = THREE.Scene,
PerspectiveCamera = THREE.PerspectiveCamera,
Mesh = THREE.Mesh,
PlaneGeometry = THREE.PlaneGeometry,
MeshPhongMaterial = THREE.MeshPhongMaterial,
Color = THREE.Color,
DoubleSide = THREE.DoubleSide,
NoBlending = THREE.NoBlending,
WebGLRenderer = THREE.WebGLRenderer,
MeshBasicMaterial = THREE.MeshBasicMaterial;
var CSS3DDemo = /** @class */ (function() {
function CSS3DDemo() {
this.scene = new Scene();
this.camera = new PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 500);
this.webGLRenderer = new WebGLRenderer();
this.css3DRenderer = new CSS3DRenderer();
this.controls = new OrbitControls(this.camera, this.css3DRenderer.domElement);
this.camera.position.set(0, 0, 20);
this.webGLRenderer.setSize(window.innerWidth, window.innerHeight);
this.webGLRenderer.setClearColor(0xFFFFFF);
this.css3DRenderer.setSize(window.innerWidth, window.innerHeight);
this.css3DRenderer.domElement.style.top = '0px';
this.css3DRenderer.domElement.style.left = '0px';
this.css3DRenderer.domElement.style.position = 'absolute';
var div = document.querySelector("#stuff");
div.style.width = '160px';
div.style.height = 'auto';
var object = new CSS3DObject(div);
object.position.set(0, 0, 0);
object.scale.set(1 / 16, 1 / 16, 1 / 16);
this.scene.add(object);
var planeGeometry = new PlaneGeometry(10, 10);
this.scene.add(this.camera);
document.getElementById("content").appendChild(this.webGLRenderer.domElement);
document.getElementById("content").appendChild(this.css3DRenderer.domElement);
this.render();
}
CSS3DDemo.prototype.render = function() {
var _this = this;
window.requestAnimationFrame(function() {
return _this.render();
});
this.css3DRenderer.render(this.scene, this.camera);
this.webGLRenderer.render(this.scene, this.camera);
this.controls.update();
};
return CSS3DDemo;
}());
new CSS3DDemo();
html,
body {
width: 100vw;
margin: 0;
height: 100vh;
padding: 0;
overflow: hidden;
border: 0;
}
#content {
width: 100%;
height: 100%;
overflow: hidden;
}
<div id="content"></div>
<div id="stuff">Hello from dom</div>
<script src='https://gitcdn.xyz/repo/mrdoob/three.js/dev/build/three.min.js'></script>
<script src='https://gitcdn.xyz/repo/mrdoob/three.js/dev/examples/js/controls/OrbitControls.js'></script>
<script src='https://gitcdn.xyz/repo/mrdoob/three.js/dev/examples/js/renderers/CSS3DRenderer.js'></script>
<script src="./script.js"></script>
Thaaaaaanks!!!!!!!!